package com.ffs.template.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * saxReader工具
 */
public class SaxUtil {

    private static Document document;

    private static Element rootElement;

    private static String tableName;

    private static String primaryKey;

    private static String aPackage;

    private static String desc;

    static {
        try {
            SAXReader saxReader = new SAXReader();
            InputStream config = SaxUtil.class.getClassLoader().getResourceAsStream("test.xml");
            document = saxReader.read(config);
            rootElement = document.getRootElement();
            tableName = rootElement.attributeValue("name");
            //主键
            primaryKey = rootElement.attributeValue("id");
            //存放包名
            aPackage = rootElement.attributeValue("package");
            //描述
            desc = rootElement.attributeValue("desc");
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询
     */
    public void parseXmlSelect() {
        Iterator<Element> select = rootElement.elementIterator("select");
        while (select.hasNext()) {
            List<String> fields = new ArrayList<>();
            Element next = select.next();
            Iterator<Element> field = next.elementIterator("field");
            while (field.hasNext()){

            }
            System.out.println(next.attributeValue("id"));
        }
    }

    /**
     * 插入
     */
    public void parseXmlInsert() {

    }

    /**
     * 更新
     */
    public void parseXmlUpdate() {

    }

    public static void main(String[] args) {
        SaxUtil saxUtil = new SaxUtil();
        saxUtil.parseXmlSelect();
    }
}
